<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	<h4 class="modal-title">Item: {{ $orderComponent->component->name }}</h4>
</div>
<div class="modal-body">
	<div class="alert alert-danger alert-dismissable" id="actionError" style="display:none;">
	    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
	    <span></span>
	</div>
	<div class="col-sm-6">
		<dl class="dl-horizontal">
	  		<dt>Cliente:</dt>
	  		<dd>{{ $orderComponent->order->customer->name }}</dd>
			<dt>Nro Pedido:</dt>
	  		<dd>{{ $orderComponent->order->order_number }}</dd>
			<dt>Ordem de Produção:</dt>
	  		<dd>{{ $orderComponent->order->production_number }}</dd>
	  		<dt>Quantidade:</dt>
	  		@if (!empty($orderComponent->order->amount) && !empty($orderComponent->amount))
	  			<dd>{{ $orderComponent->amount * $orderComponent->order->amount }}</dd>
	  		@else
	  			<dd>Não informada</dd>
	  		@endif
		</dl>
	</div>
	<div class="col-sm-6">
		<dl class="dl-horizontal">
	  		<dt>Entrada:</dt>
	  		<dd>{{ $orderComponent->order->order_date->format('d/m/Y') }}</dd>
	  		<dt>Início da Produção:</dt>
	  		<dd>{{ $orderComponent->order->production_start_date->format('d/m/Y') }}</dd>
	  		<dt>Data de Entrega:</dt>
	  		<dd>{{ $orderComponent->order->production_end_date->format('d/m/Y') }}</dd>
		</dl>
	</div>
	
  	<table class="table table-condensed">
		<thead>
			<tr>
				@if (in_array('admin', $userPrivileges) || in_array('cc_admin', $userPrivileges))
					<th></th>
				@endif
				<th>Centro de Custo</th>
				<th>Entrada Prevista</th>
				<th>Saída Prevista</th>
				<th>Saída Real</th>
				<th>Responsável</th>
			</tr>
		</thead>
		<tbody>
			@foreach ($ccDates as $key => $item)
				@if (($key + 1) < $orderComponent->current_step)
		  			<tr class="success">
		  		@elseif ($item->is_late)
		  			<tr class="danger">
		  		@elseif (($key + 1) === $orderComponent->current_step)
		  			<tr>
		  		@else
		  			<tr>
		  		@endif
		  			@if (in_array('admin', $userPrivileges) || in_array('cc_admin', $userPrivileges))
			  			<td>
							{{ Form::radio('current_step', $key + 1, ($key + 1) === $orderComponent->current_step) }}
			  			</td>
		  			@endif
		  			<td>
		  				{{ $item->cc_name }}
		  				@if (($key + 1) === $orderComponent->current_step)
		  					<span class="glyphicon glyphicon-flag" style="color: black; font-style: italic"></span>
		  				@endif
		  			</td>
		  			<td>
		  				{{ date("d/m/Y", strtotime($item->previous_probable_date)) }}
		  			</td>
		  			<td>
		  				{{ date("d/m/Y", strtotime($item->probable_date)) }}
		  			</td>
		  			@if (($key + 1) < $orderComponent->current_step)
			  			<td>
			  				@if (!empty($item->log_username))
			  					{{ date("d/m/Y H:i",strtotime($item->log_created_at)) }}
							@endif
			  			</td>
			  			<td>
			  				{{ $item->log_username }} 
			  			</td>
			  		@else
			  			<td></td>
			  			<td></td>
			  		@endif
		  		</tr>
			@endforeach
		</tbody>
	</table>
  	<span style="font-size: 12px;">O ícone <span class="glyphicon glyphicon-flag" style="color: black; font-size: 14px; font-style: italic"></span> marca o Centro de Custo Atual</span>
</div>

<div class="modal-footer">
	<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
	@if ($hasCurrentCostsCentrePrivilege)
		@if (in_array('admin', $userPrivileges) || in_array('cc_admin', $userPrivileges))
			<button type="button" class="btn btn-primary" id="changeCostsCentre">Alterar Centro de Custo</button>
			@if ($orderComponent->current_step == count($ccDates))
				<button type="button" class="btn btn-primary" id="markCompleted">Concluir</button>
			@endif
		@else
			@if ($orderComponent->current_step == count($ccDates))
				<button type="button" class="btn btn-primary" id="markCompleted">Concluir</button>
			@else
				<button type="button" class="btn btn-primary" id="advanceCostsCentre">Avançar Centro de Custo</button>
			@endif
		@endif
	@endif
</div>
<script>
	$(document).ready(function() {
		var orderComponentId = {{ $orderComponent->id }};
		
		$('#changeCostsCentre').click(function(e) {
			$.post(
				$.rootUrl + 'orderComponent/change-step',
				{id: orderComponentId, step: $('input[name="current_step"]:checked').val()},
				function(data) {
					if (data.success) {
						$('#mdl').modal('toggle');
						$('.reportComponents').dashboard('updateTables');
					} else {
						$('#actionError').find('span').html(data.message);
						$('#actionError').show();
					}
				}
			);	

			e.preventDefault();
		});

		$('#advanceCostsCentre').click(function(e) {
			$.post(
				$.rootUrl + 'orderComponent/advance-step',
				{id: orderComponentId},
				function(data) {
					if (data.success) {
						$('#mdl').modal('toggle');
						$('.reportComponents').dashboard('updateTables');
					} else {
						$('#actionError').find('span').html(data.message);
						$('#actionError').show();
					}
				}
			);	

			e.preventDefault();
		});

		$('#markCompleted').click(function(e) {
			$.post(
				$.rootUrl + 'orderComponent/mark-complete',
				{id: orderComponentId},
				function(data) {
					if (data.success) {
						$('#mdl').modal('toggle');
						$('.reportComponents').dashboard('updateTables');
					} else {
						$('#actionError').find('span').html(data.message);
						$('#actionError').show();
					}
				}
			);	

			e.preventDefault();
		});
	});
</script>